{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "61c580db",
   "metadata": {},
   "outputs": [],
   "source": [
    "import sisl\n",
    "import numpy as np\n",
    "import matplotlib.pyplot as plt\n",
    "%matplotlib inline"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "8d217005",
   "metadata": {},
   "source": [
    "Quantum Hall Effect\n",
    "-----------------------------\n",
    "\n",
    "In this exercise, we will build on [TB_07](../TB_07/run.ipynb) to simulate the quantum hall effect.\n",
    "\n",
    "Here, we will extract the Hall resistance from the transmissions calculated with TBtrans using the Landauer-Büttiker formalism.\n",
    "\n",
    "## Exercise Overview:\n",
    "1. Create a Hall bar\n",
    "2. Construct Hamiltonians and add magnetic fields (see [TB_07](../TB_07/run.ipynb))\n",
    "3. Calculate the transmission with `tbtrans`\n",
    "4. Extract the Hall resistance $R_H$.\n",
    "\n",
    "## Exercise"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "149b3215",
   "metadata": {},
   "source": [
    "### 1. Create a Hall bar\n",
    "In order to be able to observe the quantum hall effect, the size of the hall bar needs to be big enough. For a 4(6) lead device, reasonable dimensions are: \n",
    "1. 4-lead device:\n",
    "   - Width of electrodes (perpendicular to the semi-infinite axis): 30 atoms\n",
    "   - Offset of the electrodes 2(3) from the corner of the central part: >= 5 atoms\n",
    "\n",
    "\n",
    "2. 6-lead device: \n",
    "   - Width of electrodes (perpendicular to the semi-infinite axis): 30 atoms\n",
    "   - Spacing between electrodes on the same side: 50 atoms\n",
    "   - Offset of the electrodes 2(3,5,6) from the corner of the central part: > 15 atoms\n",
    "\n",
    "|4-lead device |6-lead device |\n",
    "|:-------------|:-------------|\n",
    "|<img src=\"img/set-up-4.png\" alt=\"4 lead hall bar\" style=\"width: 310px;\"/>|<img src=\"img/set-up-6.png\" alt=\"6 lead hall bar\" style=\"width: 490px;\"/>|"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "a30991dc",
   "metadata": {},
   "outputs": [],
   "source": [
    "# Create a Hall bar"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "57fc7cde",
   "metadata": {},
   "source": [
    "### 2. Construct Hamiltonian and add magnetic fields\n",
    "\n",
    "The required field strengths may vary depending on the size of the hall bar.\n",
    "A good starting point might be `B = 1 / np.arange(1,31)`"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "f8745b36",
   "metadata": {},
   "outputs": [],
   "source": [
    "def peierls(B=0):\n",
    "    def peierls(self, ia, atoms, atoms_xyz=None):\n",
    "        idx = self.geometry.close(ia, R=[0.1, 1.01], atoms=atoms, atoms_xyz=atoms_xyz)\n",
    "        # Onsite\n",
    "        self[ia, idx[0]] = 4\n",
    "    \n",
    "        # Hopping\n",
    "        if B == 0:\n",
    "            self[ia, idx[1]] = -1\n",
    "        else:\n",
    "            xyz = self.geometry.xyz[ia]\n",
    "            dxyz = self.geometry[idx[1]]\n",
    "            self[ia, idx[1]] = - np.exp(-0.5j * B * (dxyz[:, 0] - xyz[0])*(dxyz[:,1] + xyz[1]))\n",
    "    return peierls"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "6135dc86",
   "metadata": {},
   "outputs": [],
   "source": [
    "# H0 = sisl.Hamiltonian(geom, dtype=np.float64)\n",
    "# H0.construct(peierls()))\n",
    "#\n",
    "# HB = sisl.Hamiltonian(geom, dtype=np.complex128)\n",
    "# HB.construct(peierls(B))\n",
    "# \n",
    "# dH = ... "
   ]
  },
  {
   "cell_type": "markdown",
   "id": "bac95ee9",
   "metadata": {},
   "source": [
    "### 3. Calculate the transmission with TBtrans\n",
    "\n",
    "The folder of this exercise contains the skeleton of an input file for a 4-lead (`RUN-4.fdf`) and 6-lead device (`RUN-6.fdf`), as well as a script to run TBtrans for all values of the magnetic field (`run.sh`).\n",
    "\n",
    "*Depending on the size of the Hall bar, this step might require some a considerable amount of time.*\n",
    "\n",
    "### 4. Extract the Hall resistance $(R_H)$\n",
    "\n",
    "The Hall resistance ($R_H$) in a 4 lead Hall bar like the one shown above is given by\n",
    "\n",
    "$$R_H = \\left.\\frac{V_2-V_3}{I_1}\\right|_{I_2 = I_3 = 0}.$$\n",
    "\n",
    "In order to find a relationship between the transmission curves and the hall resistance, we express the lead currents $I_i$ in terms of applied biases $V_i$ and the transmissions $T_{ij}$ between leads $i$ and $j$\n",
    "$$I_i = \\sum_j G_{ij} (V_i - V_j)\\quad\\text{where}\\quad G_{ij} = \\frac{2e^2}{h} T_{ij}.$$\n",
    "Since the currents only depend on bias differences, we can set one of them to zero without loss of generality (here $V_3 = 0$). This allows us to rewrite the relation as\n",
    "\n",
    "$$\\mathbf{I} = \\mathcal{G} \\mathbf{V}\n",
    "\\quad\\text{, where}\\quad\n",
    "\\mathcal{G}_{ii} = \\sum_{i\\neq j} G_{ij}\n",
    "\\quad\\text{and}\\quad\n",
    "\\mathcal{G}_{ij} = - G_{ij}.$$\n",
    "\n",
    "Using the inverse $\\mathbf{R}$ of $\\mathcal{G}$, we can express $V_2$ and $V_3$ in terms of the lead currents $I_i$ and calculate the Hall conductance:\n",
    "\n",
    "$$\n",
    "V_i = R_{i1} I_1 + R_{i2} I_2 + R_{i3} I_3,\n",
    "$$\n",
    "and finally, we find the Hall resistance:\n",
    "$$\n",
    "R_H = R_{21}-R_{31}\n",
    "$$\n",
    "\n",
    "The derivation for the 6-lead device is analogous and yields:\n",
    "$$\n",
    "R_H = R_{21}-R_{61}\n",
    "$$"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "bcf811aa",
   "metadata": {},
   "outputs": [],
   "source": [
    "# Create short-hand function to open files\n",
    "gs = sisl.get_sile\n",
    "# No magnetic field\n",
    "tbt0 = gs('M_0/siesta.TBT.nc')\n",
    "# All magnetic fields in increasing order\n",
    "tbts = [gs('M_{}/siesta.TBT.nc'.format(rec_phi)) for rec_phi in rec_phis]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "d3a5bc28",
   "metadata": {},
   "outputs": [],
   "source": [
    "def G_matrix(tbtsile):\n",
    "    # Construct G\n",
    "  \n",
    "G0 = G_matrix(tbt0)\n",
    "# Remove one row and column from the matrix\n",
    "G0 = np.delete(np.delete(G0, ..., axis=...), ..., axis=...)\n",
    "R0 = np.linalg.inv(G)\n",
    "\n",
    "G = ...\n",
    "R = ...\n",
    "\n",
    "RH = ..."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "528277a9",
   "metadata": {},
   "outputs": [],
   "source": [
    "# Plot the hall resitance\n",
    "#  - as a function of energy for a fixed magnetic field strength\n",
    "#  - as a funciton of the magnetic field strength for a fixed energy"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b4bc7251",
   "metadata": {},
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
